package com.mei.offer;

/**
 * 将一个字符串中的空格替换成“%20”
 */
public class Test4 {
    public static void main(String[] args) {
        String string = "How are you, hello World!";
        String result = translate(string);
        System.out.println(result);
    }

    private static String translate(String string) {
        if (null == string) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        int length = string.length() - 1;
        for (int i = 0; i < length; i++) {
            if (String.valueOf(string.charAt(i)).equals(" ")) {
                sb.append("%20");
            } else {
                sb.append(string.charAt(i));
            }
        }
        return sb.toString();
    }
}